package com.leyou.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author: HuYi.Zhang
 * @create: 2018-04-24 17:20
 **/
public class JsonUtils {

    public static final ObjectMapper mapper = new ObjectMapper();

    private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);

    @Nullable
    public static String serialize(Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj.getClass() == String.class) {
            return (String) obj;
        }
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            logger.error("json序列化出错：" + obj, e);
            return null;
        }
    }

    @Nullable
    public static <T> T parse(String json, Class<T> tClass) {
        try {
            return mapper.readValue(json, tClass);
        } catch (IOException e) {
            logger.error("json解析出错：" + json, e);
            return null;
        }
    }

    @Nullable
    public static <E> List<E> parseList(String json, Class<E> eClass) {
        try {
            return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, eClass));
        } catch (IOException e) {
            logger.error("json解析出错：" + json, e);
            return null;
        }
    }

    @Nullable
    public static <K, V> Map<K, V> parseMap(String json, Class<K> kClass, Class<V> vClass) {
        try {
            return mapper.readValue(json, mapper.getTypeFactory().constructMapType(Map.class, kClass, vClass));
        } catch (IOException e) {
            logger.error("json解析出错：" + json, e);
            return null;
        }
    }

    @Nullable
    public static <T> T nativeRead(String json, TypeReference<T> type) {
        try {
            return mapper.readValue(json, type);
        } catch (IOException e) {
            logger.error("json解析出错：" + json, e);
            return null;
        }

    }

    public static void main(String[] args) {
        //Map<String, String> generic = JsonUtils.nativeRead("{\"1\":\"360\",\"2\":\"N6 Lite\",\"3\":\"2017.0\",\"5\":\"160\",\"6\":\"其他\",\"7\":\"Android\",\"8\":\"骁龙（Snapdragon)\",\"9\":\"骁龙630\",\"10\":\"八核\",\"11\":\"2.2\",\"14\":\"5.5\",\"15\":\"1920*1080\",\"16\":\"800.0\",\"17\":\"1300.0\",\"18\":\"4020\"}",
        //        new TypeReference<Map<String, String>>() {
        //        });
        //Map<String, String> generic = JsonUtils.parseMap("{\"1\":\"360\",\"2\":\"F5\",\"3\":\"2017.0\",\"5\":\"135\",\"6\":\"其他\",\"7\":\"Android\",\"8\":\"展讯\",\"9\":\"其他\",\"10\":\"四核\",\"11\":\"1.3\",\"14\":\"5.0\",\"15\":\"1280×720（HD）\",\"16\":\"200.0\",\"17\":\"500.0\",\"18\":\"2000.0\"}", String.class, String.class);

        Map<String, List> stringListMap = JsonUtils.parseMap("{\"4\":[\"极夜黑\",\"深海蓝\",\"钛泽银\"],\"12\":[\"6GB\"],\"13\":[\"64GB\"]}", String.class, List.class);

        System.out.println(stringListMap);
    }


}
